package com.tech.platform.cms.databases.mybatis.config;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.tech.platform.cms.databases.mybatis.bean.TenantProperties;
import com.tech.platform.cms.databases.mybatis.sqlParser.DynamicTableParser;
import com.tech.platform.cms.databases.mybatis.sqlParser.tenantParser.TenantColumnSqlParser;
import com.tech.platform.cms.databases.mybatis.sqlParser.tenantParser.TenantTableSqlParser;
import com.tech.platform.cms.databases.mybatis.sqlParser.tenantParser.handler.NewTenantTableHandler;
import com.tech.platform.cms.databases.mybatis.sqlParser.tenantParser.handler.TenantColumnHandler;
import com.tech.platform.cms.databases.mybatis.sqlParser.tenantParser.handler.TenantTableHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by Administrator on 2019/12/16.
 */
@Configuration
@Slf4j
public class DBMybatisConfiguration {

    @Autowired
    private MybatisExtendProperties mybatisExtendProperties;

    /**
     * 添加带有租户的分页插件
     * @return
     */
    @Bean
    @ConditionalOnProperty(prefix = "mybatis-extend",name = "use-tenant",havingValue = "true")
    public PaginationInterceptor tenantPaginationInterceptor(){
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        TenantProperties tenantProperties = mybatisExtendProperties.getTenantProperties();
        if(ObjectUtil.isEmpty(tenantProperties)){
            return paginationInterceptor;
        }

        String sqlParser = tenantProperties.getIsolationType();
        switch (sqlParser){
            case "column": //字段隔离
                TenantColumnSqlParser columnSqlParse = new TenantColumnSqlParser();
                columnSqlParse.setTenantHandler(
                        new TenantColumnHandler().setUseMultipleTenantId(tenantProperties.isUseMultipleTenantId()).
                                setFilterTables(tenantProperties.getFilterTables()));
                paginationInterceptor.setSqlParserList(CollUtil.newArrayList(columnSqlParse));
                break;
            case "table"://表隔离
//                TenantTableSqlParser tableSqlParser = new TenantTableSqlParser();
//                tableSqlParser.setTenantSchemaHandler(new TenantTableHandler().setFilterTables(tenantProperties.getFilterTables()));
//                paginationInterceptor.setSqlParserList(CollUtil.newArrayList(tableSqlParser));
                //使用新版表隔离策略
                DynamicTableParser tableParser = new DynamicTableParser();
                tableParser.setTableHandler(new NewTenantTableHandler().setFilterTables(tenantProperties.getFilterTables()));
                paginationInterceptor.setSqlParserList(CollUtil.newArrayList(tableParser));
                break;

        }
        return paginationInterceptor;
    }

    @Bean
    @ConditionalOnMissingBean(PaginationInterceptor.class)
    public PaginationInterceptor taginationInterceptor(){
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        return paginationInterceptor;
    }
}
